<%--
  Created by IntelliJ IDEA.
  User: Rui
  Date: 2018/10/26
  Time: 9:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    String imgSrc = basePath + "resources/images/noImage.jpg";
    session.setAttribute("imgSrc", imgSrc);
%>
<!DOCTYPE html>
<html style="background-color: #fff;">
<head>
    <%@include file="../../../resources/inc/head.jsp" %>
</head>
<body>
<div class="layui-form" lay-filter="layuiadmin-form-contentFriendlyLink" id="layuiadmin-form-contentFriendlyLink"
     style="padding: 20px;">
    <c:if test="${type==1||type==2}">
    <div class="layui-form-item">
        <label class="layui-form-label">封面</label>
        <div class="layui-input-block">

            <div class="layui-upload">
                <input type="hidden" value="${contentArticle.photo==null?imgSrc:contentArticle.photo}" name="photo" id="photo" lay-verify="required" class="layui-input">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" width="120px" src="${contentArticle.photo==null?imgSrc:contentArticle.photo}" id="photoImg">
                </div>
                <button type="button" class="layui-btn" id="photoBth"><i class="layui-icon layui-icon-upload"></i>上传图片</button>

            </div>   <div class="layui-form-mid layui-word-aux">上传图片，支持.jpg、.jpeg、.png格式照片，单张大小不超过1M,不要超过一张</div>
        </div>
    </div>
    </c:if>
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <input type="text" value="${contentArticle.title}" name="title" lay-verify="required" placeholder=""
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <input type="hidden" value="${contentArticle.id}" name="id">
        <label class="layui-form-label">分类</label>
        <div class="layui-input-block">
            <select name="categoryId">
                <c:forEach items="${category}" var="category">
                    <option value="${category.id}" ${contentArticle.categoryId==category.id?'selected':''}> ${category.name}</option>
                </c:forEach>
            </select>
        </div>
    </div>
    <c:if test="${type==2}">
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select name="typeId">
                        <option value="0" ${contentArticle.typeId==0?'selected':''}>
                               医疗
                        </option>
                    <option value="1" ${contentArticle.typeId==1?'selected':''}>
                        体育
                    </option>
                    <option value="2" ${contentArticle.typeId==2?'selected':''}>
                        政府
                    </option>
                    <option value="3" ${contentArticle.typeId==3?'selected':''}>
                        其他
                    </option>
                </select>
            </div>
        </div>
    </c:if>
    <c:if test="${type==1}">
    <div class="layui-form-item">
        <label class="layui-form-label">作者</label>
        <div class="layui-input-block">
            <input type="text" value="${contentArticle.author}" name="author" lay-verify="required" placeholder=""
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    </c:if>
    <div class="layui-form-item">
        <label class="layui-form-label">副标题</label>
        <div class="layui-input-block">
            <textarea type="text"  name="description" lay-verify="required" placeholder="不要超过一千字"
                      autocomplete="off" class="layui-textarea">${contentArticle.description}</textarea>
        </div>
    </div>
<c:if test="${type==3}">
    <div class="layui-form-item">
        <label class="layui-form-label">简要描述</label>
        <div class="layui-input-block">
            <textarea type="text"  name="keyword" lay-verify="required" placeholder="描述不要超过一千字"
                      autocomplete="off" class="layui-textarea">${contentArticle.keyword}</textarea>
        </div>
    </div>
</c:if>
    <%--<div class="layui-form-item">--%>
        <%--<label class="layui-form-label">关键字</label>--%>
        <%--<div class="layui-input-block">--%>
            <%--<input type="text" value="${contentArticle.keyword}" name="keyword" placeholder="" autocomplete="off"--%>
                   <%--class="layui-input">--%>
        <%--</div>--%>
    <%--</div>--%>
    <div class="layui-form-item">
        <label class="layui-form-label">正文</label>
        <div class="layui-input-block">
            <expect-x:UEditor   uEditorId="UEditor" uEditorContent="${contentArticle.content}"/>            </div>
    </div>




    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="LAY-submit" id="LAY-submit" value="确认">
    </div>
</div>

<%@include file="../../../resources/inc/footer.jsp" %>

<script>
    layui.config({
        base: '<%=basePath%>resources/lib/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['layedit', 'index', 'form', 'upload'], function () {
        console.log(UEditorUE.getContent())
        var $ = layui.$
            , form = layui.form
            , upload = layui.upload,
            layedit = layui.layedit;
        var photoUploaderInst = upload.render({
            elem: '#photoBth'
            , url: '<%=basePath%>/manager/file/upload.action?catalog=image&fileType=image&sizeLimit=1&oss=alioss'
            , before: function (obj) {
                obj.preview(function (index, file, result) {
                    $('#photoImg').attr('src', result);
                });
            }
            , done: function (res) {
                if(!res.success){
                    return layer.msg(res.message);
                }else{
                    $("#photoImg").attr("src",res.url)
                    $("#photo").val(res.url);
                }
            }
            , error: function () {
                layer.msg("上传失败!");
            }
        });
        $("#zhcontent").blur(function () {
            alert()
        })
    });
    var layedit=null;
    var zhcontentEdit=null;
    layui.use('layedit', function(){
        layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '<%=basePath%>/manager/file/upload.action?catalog=image&fileType=image&sizeLimit=1&oss=alioss' //接口url
                ,type: 'POST' //默认post
            }
        });
        zhcontentEdit=layedit.build('zhcontent'); //建立编辑器
    });

    function createSubmit() {
        var layer=layui.layer;
        var submitID = 'LAY-submit';
        layui.form.on('submit('+ submitID +')', function(data){
            var formData = data.field;
            formData.content=UEditorUE.getContent();
            formData.keyword = formData.content.substring(0, 100);
            if(formData.id==null||formData.id==""){
                formData.id = null;
                $.ajax({
                    url:"<%=basePath%>manager/content/article/create.action",
                    type:"POST",
                    data:formData,
                    dataType:"JSON",
                    success:function (data) {
                        layer.closeAll('loading');
                        if(data.success){
                            layer.msg("创建成功,1秒后跳转")
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                parent.layer.close(index);
                            },1000);
                        }else{
                            layer.msg(data.msg, {icon: 5});
                        }
                    }
                });
            }else{

                $.ajax({
                    url: "<%=basePath%>manager/content/article/update.action",
                    type:"POST",
                    data:formData,
                    dataType:"JSON",
                    success:function (data) {
                        layer.closeAll('loading');
                        if(data.success){
                            layer.msg("修改成功,1秒后跳转")
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                parent.layer.close(index);
                            },1000);
                        }else{
                            layer.msg(data.msg, {icon: 5});
                        }
                    }
                });
            }
        })
        $("#"+submitID).trigger('click');
    }
</script>
</body>
</html>
